unit untCadUsuariosAcesso;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.Buttons, Vcl.ActnList,
  Vcl.ImgList, Vcl.Grids, Vcl.DBGrids, untDTMPrincipal, Data.DB, Data.Win.ADODB,
  VarGlobais, ClassBase;

type
  TfrmCadUsuariosAcesso = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Panel4: TPanel;
    DBGrid1: TDBGrid;
    img: TImageList;
    ActionList1: TActionList;
    acIncluir: TAction;
    acExcluir: TAction;
    Panel3: TPanel;
    Panel5: TPanel;
    SpeedButton2: TSpeedButton;
    SpeedButton1: TSpeedButton;
    DBGrid2: TDBGrid;
    qryAcessos: TADOQuery;
    dtsAcessos: TDataSource;
    qryFormularios: TADOQuery;
    dtsFormularios: TDataSource;
    qryFormulariosID: TAutoIncField;
    qryFormulariosFORMULARIO: TStringField;
    qryFormulariosDESCRICAO: TStringField;
    qryAcessosID_USUARIO: TIntegerField;
    qryAcessosID_FORMULARIO: TIntegerField;
    qryAcessosID: TAutoIncField;
    qryAcessosFORMULARIO: TStringField;
    qryAcessosDESCRICAO: TStringField;
    procedure FormShow(Sender: TObject);
    procedure acIncluirExecute(Sender: TObject);
    procedure acExcluirExecute(Sender: TObject);
  private
    { Private declarations }
    Base : TBase;
    FUsuario : Integer;
    procedure atualizaFormularios;
    procedure atualizaAcessos;
    procedure insereAcesso;
    procedure excluiAcesso;
  public
    { Public declarations }
    property Usuario : Integer read FUsuario write FUsuario;
  end;

var
  frmCadUsuariosAcesso: TfrmCadUsuariosAcesso;

implementation

{$R *.dfm}

procedure TfrmCadUsuariosAcesso.acExcluirExecute(Sender: TObject);
begin
  if not qryAcessos.IsEmpty then
    excluiAcesso();
end;

procedure TfrmCadUsuariosAcesso.acIncluirExecute(Sender: TObject);
begin
  if not qryFormularios.IsEmpty then
    insereAcesso();
end;

procedure TfrmCadUsuariosAcesso.atualizaAcessos;
begin
  qryAcessos.Close();
  qryAcessos.Parameters.ParamByName('ID_USUARIO').Value := FUsuario;
  qryAcessos.Open();
end;

procedure TfrmCadUsuariosAcesso.AtualizaFormularios;
begin
  qryFormularios.Close();
  qryFormularios.Parameters.ParamByName('ID_USUARIO').Value := FUsuario;
  qryFormularios.Open();
end;

procedure TfrmCadUsuariosAcesso.excluiAcesso;
begin
  Base.SqlExec(format('DELETE FROM USUARIOS_ACESSOS '+
                      ' WHERE ID_USUARIO = %d       '+
                      '   AND ID_FORMULARIO = %d    ',
                      [FUsuario,
                       qryFormulariosID.AsInteger]));
  atualizaFormularios();
  atualizaAcessos();
end;

procedure TfrmCadUsuariosAcesso.FormShow(Sender: TObject);
begin
  atualizaFormularios();
  atualizaAcessos();
end;

procedure TfrmCadUsuariosAcesso.insereAcesso;
begin
  Base.SqlExec(format('INSERT INTO USUARIOS_ACESSOS(ID_USUARIO,ID_FORMULARIO)'+
               'VALUES(%d,%d)',
               [FUsuario,
                qryFormulariosID.AsInteger]));
  AtualizaFormularios();
  atualizaAcessos();
end;

end.
